package com.mrogrape.notepad.activity.action.item;

import java.awt.event.ActionEvent;

import javax.swing.Action;

import org.fife.ui.rtextarea.SearchContext;

import com.mrogrape.notepad.activity.action.NotepadAction;
import com.mrogrape.notepad.data.ViewContext;
import com.mrogrape.notepad.ui.editor.TextEditor;
import com.mrogrape.util.StringUtils;


public class FindNextMenuItemAction extends NotepadAction {

	/** serialVersionUID */
	private static final long serialVersionUID = -1L;

	public FindNextMenuItemAction(){

		super.putValue(Action.NAME, "Find Next");
		super.putValue(Action.SHORT_DESCRIPTION, "Find and select next matche.");
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {

		TextEditor editor = ViewContext.getMainFrame().getCurrentTextEditor();

		if(editor != null){

			String text = editor.getSelectedText();
			if(StringUtils.isEmpty(text)){
				String lastFind = ViewContext.getFindAndReplaceDialog().getFindContent();
				if(StringUtils.isEmpty(lastFind)){
					return;
				}
				text = lastFind;
			}else{
				ViewContext.getFindAndReplaceDialog().setFindContent(text);
			}
			
			this.searchContext.setSearchFor(text);
			editor.find(this.searchContext, true);
		}
	}

	SearchContext searchContext = new SearchContext();
}
